# -*- Coding:UTF-8 -*-
"""
PySimplesg库练习测试
author: Mr Liu
version: 1.0
"""
import os
import sys
import PySimpleGUI as sg


def demo1():
    layout = [[sg.Text("AutoTestCenter")],
              [sg.Text("test1")],
              [sg.Text("test2"), sg.InputText(tooltip='用户名')],
              [sg.Text("test3"), sg.InputText(password_char='*', tooltip='密码')],
              [sg.Button('Ok'), sg.Button('Cancel')]]
    window = sg.Window('Test_Title', layout)

    while True:
        event, values = window.read()
        if event in (None, 'Cancel'):
            break
        elif event in (None, 'Ok'):
            print(str(event))
            print(values)
        else:
            print(values)
    window.close()


def demo2():
    layout = [[sg.Text('工厂软件'), sg.InputText(key='fac_soft'), sg.FileBrowse('浏览')],
              [sg.Text('客户软件'), sg.InputText(key='cus_soft'), sg.FileBrowse('浏览')],
              [sg.Button('OK'), sg.Button('Cancel'), sg.Button('Reset')]]

    window = sg.Window("软件选择", layout)

    while True:
        event, value_dict = window.read()
        if event in (None, "Cancel"):
            print(value_dict)
            window.close()
            break
        elif event in "OK":
            fac_soft = value_dict['fac_soft']
            cus_soft = value_dict['cus_soft']
            if os.path.isfile(fac_soft):
                print("factory ok")
            else:

                pass
            if os.path.isfile(cus_soft):
                print("customer ok")
            else:
                print("not a file")
                pass


def demo3():
    if len(sys.argv) == 1:
        layout = [[sg.Text('Document to open')],
                  [sg.In(), sg.FileBrowse()],
                  [sg.Open(), sg.Cancel()]]

        window = sg.Window('My Script', layout)
        event, values = window.Read()
        window.Close()

        fname = values[0]
        print(event, values)
    else:
        fname = sys.argv[1]

    if not fname:
        sg.Popup("Cancel", "No filename supplied")
        raise SystemExit("Cancelling: no filename supplied")
    else:
        sg.Popup('The filename you chose was', fname)


def demo4():
    layout = [[sg.Text('Stopwatch', size=(20, 2), justification='center')],
              [sg.Text('', size=(10, 2), font=('Helvetica', 20), justification='center', key='_OUTPUT_')],
              [sg.T(' ' * 5), sg.Button('Start/Stop', focus=True), sg.Quit()]]

    window = sg.Window('Stopwatch Timer', layout)

    timer_running, i = True, 0

    while True:  # Event Loop
        event, values = window.Read(timeout=10)  # Please try and use as high of a timeout value as you can
        if event is None or event == 'Quit':  # if user closed the window using X or clicked Quit button
            break
        elif event == 'Start/Stop':
            timer_running = not timer_running
        if timer_running:
            window.Element('_OUTPUT_').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
            i += 1


def demo5():
    # The callback functions
    def button1():
        print('Button 1 callback')

    def button2():
        print('Button 2 callback')

    # Lookup dictionary that maps button to function to call
    func_dict = {'btn1': button1, '2': button2}

    # Layout the design of the GUI
    layout = [[sg.Text('Please click a button', auto_size_text=True)],
              [sg.Button('1', key='btn1'), sg.Button('2'), sg.Quit()]]

    # Show the Window to the user
    window = sg.Window('Button callback example', layout)

    # Event loop. Read buttons, make callbacks
    while True:
        # Read the Window
        event, value = window.Read()
        if event in ('Quit', None):
            break
        # Lookup event in function dictionary
        try:
            print(event)
            func_to_call = func_dict[event]  # look for a match in the function dictionary
            func_to_call()  # if successfully found a match, call the function found
        except:
            pass

    window.Close()

    # All done!
    sg.PopupOK('Done')


def demo6():
    layout = [[sg.Text('Robotics Remote Control')],
              [sg.T(' ' * 10), sg.RealtimeButton('Forward')],
              [sg.RealtimeButton('Left'), sg.T(' ' * 15), sg.RealtimeButton('Right')],
              [sg.T(' ' * 10), sg.RealtimeButton('Reverse')],
              [sg.T('')],
              [sg.Quit(button_color=('black', 'orange'))]
              ]

    window = sg.Window('Robotics Remote Control', layout, auto_size_text=True)

    #
    # Some place later in your code...
    # You need to perform a Read or Refresh on your window every now and then or
    # else it will appear your program has hung
    #
    # your program's main loop
    while True:
        # This is the code that reads and updates your window
        event, values = window.Read(timeout=10)
        if event is not None:
            print(event)
        if event == 'Quit' or values is None:
            break

    window.Close()  # Don't forget to close your window!


def main():
    # demo1()
    # demo2()
    # demo3()
    # demo4()
    # demo5()
    demo6()


if __name__ == '__main__':
    main()